package com.example.myblog.controller.admin;

import com.example.myblog.entity.Tag;
import com.example.myblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class TagController {
    @Autowired
    private TagService tagService;
    @GetMapping("/tags")//去列表页
    public String tags(Model model,Integer page){
        page = page==null?0:page;
        model.addAttribute("page",tagService.pageAll(page));
        return "admin/tags";
    }

    @GetMapping("/tags/input")//新增---去input页
    public String input(Model model){
        Tag tag = new Tag();
        model.addAttribute("tag",tag);
        return "admin/tags-input";
    }

    @PostMapping("/tags")//保存新增的tag后---去列表页
    public String save(Tag tag){
        tagService.save(tag);
        return "redirect:/admin/tags";
    }

    @PostMapping("/tags/{id}")//修改tag-name为传入的name后----------去列表页
    public String update(@PathVariable Integer id,String name){
        Tag tag = tagService.findById(id);
        tag.setName(name);
        tagService.update(tag);
        return "redirect:/admin/tags";
    }

    @GetMapping("/tags/{id}/input")//去往编辑页修改tag-name
    public String editor(@PathVariable Integer id,Model model){
        Tag tag = tagService.findById(id);
        model.addAttribute("tag",tag);
        return "admin/tags-input";
    }

    @GetMapping("/tags/{id}/delete")//删除tag之后---去往列表页
    public String delete(@PathVariable Integer id){
        tagService.deleteById(id);
        return "redirect:/admin/tags";
    }


}
